/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tiberiumlabs.screenshot;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws AWTException {

        System.setProperty("awt.useSystemAAFontSettings", "on");
        System.setProperty("swing.boldMetal", "false");

        try {
            String os = System.getProperty("os.name");
            if (os != null && os.indexOf("Windows") != -1) {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        } catch (Exception ignore) {
        }

        ImageComponent imageComponent = new ImageComponent();
        Robot robot = new Robot();
        imageComponent.setImage(robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())));

        JFrame frame = new JFrame("Screenshot");
        JPanel optionsPanel = new JPanel();
        JPanel buttonsPanel = new JPanel();
        JPanel contentPane = new JPanel();
        JSpinner delaySpinner = new JSpinner();
        JCheckBox hideWindowCheckbox = new JCheckBox("Hide this window", true);
        ExitAction exitAction = new ExitAction(frame);

        ScreenshotAction screenshotAction = new ScreenshotAction(imageComponent, delaySpinner, hideWindowCheckbox, frame, robot);

        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.Y_AXIS));
        optionsPanel.setBorder(new TitledBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.GRAY, 1, true), BorderFactory.createEmptyBorder(6, 8, 6, 8)), "Options"));
        Box box = Box.createHorizontalBox();
        box.add(new JLabel("Screenshot delay:"));
        box.add(Box.createHorizontalStrut(6));
        box.add(delaySpinner);
        box.add(Box.createHorizontalGlue());
        optionsPanel.add(box);
        optionsPanel.add(Box.createVerticalStrut(6));
        box = Box.createHorizontalBox();
        box.add(hideWindowCheckbox);
        box.add(Box.createHorizontalGlue());
        optionsPanel.add(box);
        optionsPanel.setMaximumSize(new Dimension(optionsPanel.getMaximumSize().width, optionsPanel.getMinimumSize().height));

        buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
        buttonsPanel.add(new JButton(screenshotAction));
        buttonsPanel.add(Box.createHorizontalStrut(8));
        buttonsPanel.add(new JButton(new SaveAction(imageComponent, contentPane)));
        buttonsPanel.add(Box.createHorizontalStrut(8));
        buttonsPanel.add(Box.createHorizontalGlue());
        buttonsPanel.add(new JButton(exitAction));
        buttonsPanel.setMaximumSize(new Dimension(buttonsPanel.getMaximumSize().width, buttonsPanel.getMinimumSize().height));

        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        int outerBorder = 12;
        contentPane.setBorder(BorderFactory.createEmptyBorder(outerBorder, outerBorder, outerBorder, outerBorder));
        contentPane.setAlignmentX(JPanel.CENTER_ALIGNMENT);
        contentPane.add(imageComponent);
        contentPane.add(Box.createVerticalStrut(8));
        contentPane.add(optionsPanel);
        contentPane.add(Box.createVerticalStrut(8));
        contentPane.add(buttonsPanel);

        frame.addWindowListener(exitAction);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setMinimumSize(frame.getSize());
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2);
        frame.setVisible(true);
    }

    private static class ExitAction extends AbstractAction implements WindowListener {

        private JFrame frame;

        public ExitAction(JFrame frame) {
            this.frame = frame;
            putValue(NAME, "Quit");
        }

        public void actionPerformed(ActionEvent e) {
            if (frame != null) {
                frame.dispose();
            }
            System.exit(0);
        }

        public void windowOpened(WindowEvent e) {
        }

        public void windowClosing(WindowEvent e) {
            actionPerformed(null);
        }

        public void windowClosed(WindowEvent e) {
        }

        public void windowIconified(WindowEvent e) {
        }

        public void windowDeiconified(WindowEvent e) {
        }

        public void windowActivated(WindowEvent e) {
        }

        public void windowDeactivated(WindowEvent e) {
        }

    }
}
